package com.xizimall.search.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
/**
 * 搜索Controller
 * <p>Title: SearchController</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月24日下午9:16:11
 * @version 1.0
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xizimall.search.pojo.SearchResult;
import com.xizimall.search.service.SearchService;
import com.xizimall.common.pojo.XizimallResult;
import com.xizimall.common.utils.ExceptionUtil;
@Controller
public class SearchController {
    
	@Autowired
	private SearchService searchService;
	
	@RequestMapping(value="/query",method=RequestMethod.GET)
	@ResponseBody
	public XizimallResult search(@RequestParam("q")String 
	   queryString,@RequestParam(defaultValue="1")
	    Integer page,@RequestParam(defaultValue="60")Integer rows){
		//查询条件不能为空
		if(StringUtils.isBlank(queryString)){
			XizimallResult.build(400, "查询条件不能为空");
		}
		SearchResult searchResult=null;
		try {
			queryString=new String(queryString.getBytes("iso8859-1"),"utf-8");
			searchResult=searchService.search(queryString, page, rows);
		} catch (Exception e) {
			e.printStackTrace();
			XizimallResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		return XizimallResult.ok(searchResult);
	}
	
}
